﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using 纸片人.win2dEffectControl.DataClass;

namespace 纸片人.win2dEffectControl
{
    public class ConnectLine : Path
    {
        /// <summary>
        /// 两个连接的元素
        /// </summary>
        public ConnectLineUIElement ConnectElement;

        /// <summary>
        /// 相对的元素
        /// </summary>
        public UIElement Visual;

        private PathGeometry geometry;
        private PathFigure figure;
        private BezierSegment bezier;
        private LineSegment startLine;
        private LineSegment endLine;
        private Point startPoint;
        private Point endPoint;
        public ConnectLine()
        {
            geometry = new PathGeometry();
            figure = new PathFigure();
            bezier = new BezierSegment();
            startLine =new LineSegment();
            endLine =new LineSegment();
            this.Stroke =new SolidColorBrush( Colors.Red);
            StrokeThickness = 4;
         
            Data = geometry;
            geometry.Figures.Add(figure);
            figure.Segments.Add(startLine);
            figure.Segments.Add(bezier);
            figure.Segments.Add(endLine);

        }

        


        public ConnectLine(ConnectLineUIElement connectElement,UIElement visual) :this()
        {

            this.ConnectElement = connectElement;
            this.Visual = visual;
            DrawConnectLine();
        }

        /// <summary>
        /// 绘制连接的线段
        /// </summary>
        public void DrawConnectLine()
        {
            if (ConnectElement != null)
            {
                if (Visual != null)
                {
                    startPoint = ConnectElement.SourceElement.TransformToVisual(Visual).TransformPoint(new Point(0, 0));
                    endPoint = ConnectElement.TargetElement.TransformToVisual(Visual).TransformPoint(new Point(0, 0));
                }
                else
                {
                    startPoint = ConnectElement.SourceElement.TransformToVisual((UIElement)this.Parent).TransformPoint(new Point(0, 0));
                    endPoint = ConnectElement.TargetElement.TransformToVisual((UIElement)this.Parent).TransformPoint(new Point(0, 0));
                }
                var point1 = new Point(startPoint.X +100, startPoint.Y);
                var point2 = new Point(endPoint.X-100, endPoint.Y);

                startLine.Point = new Point(startPoint.X + 40, startPoint.Y);
                endLine.Point = endPoint;
                figure.StartPoint = startPoint;
                bezier.Point1 = point1;
                bezier.Point2 = point2;
                bezier.Point3 = new Point(endPoint.X-40,endPoint.Y);
            }
        }

        public void SetConnectElement (ConnectLineUIElement connectElement)
        {
            this.ConnectElement = connectElement;
            DrawConnectLine();
        }

    }
}
